﻿
using System;
using System.Web;
using System.Web.Mvc;
using System.Web.SessionState;
using Infrastructure.Base.Services;
using Infrastructure.Mvc.SeedWork.Models;
using Infrastructure.Mvc.SeedWork.Models.ViewContent;
using Infrastructure.Mvc.SeedWork.Resources;

namespace Infrastructure.Mvc.SeedWork.ControlContent
{
    public class ManageContext
    {
        public static readonly ManageContext Instance = new ManageContext();

        private PageModel _pageModel;

        private IAuthenUserService _userService;

        private ManageContext()
        {
        }

        private HttpContext Context
        {
            get { return HttpContext.Current; }
        }

        public UserModel User
        {
            get
            {
                try
                {
                    if (Context == null)
                        throw new Exception(Messages.ManageContext_User_SessionNotFound);
                    HttpSessionState session = Context.Session;
                    if (session != null)
                    {
                        var userModel = session[typeof (UserModel).FullName] as UserModel;
                        if (Context.Request.IsAuthenticated && userModel == null)
                        {
                            UserModel model = _userService.FindUserModel(Context.User.Identity.Name);

                            if (model == null)
                            {
                                throw new Exception(Messages.ManageContext_User_NotFound);
                            }
                            session[typeof (UserModel).FullName] = userModel = model;
                        }
                        return userModel;
                    }
                    throw new Exception(Messages.ManageContext_User_SessionNotFound);
                }
                catch (Exception)
                {
                    throw new Exception(Messages.ManageContext_User_NotFound);
                }
            }
        }

        public SiteModel Site
        {
            get
            {
                var siteModel = Context.Application[typeof (SiteModel).FullName] as SiteModel;
                if (siteModel == null)
                {
                    Context.Application[typeof (SiteModel).FullName] = siteModel = new SiteModel
                    {
                        Name = string.Empty,
                        Keywords = string.Empty,
                        Description = string.Empty
                    };
                }

                return siteModel;
            }
        }

        public PageModel Page
        {
            get
            {
                return _pageModel ?? (_pageModel = new PageModel
                {
                    Title = Site.Name,
                    Keywords = Site.Keywords,
                    Description = Site.Description,
                    UrlReferrer =
                        Context.Request.UrlReferrer != null
                            ? Context.Request.UrlReferrer.ToString()
                            : string.Empty,
                });
            }
        }

        public void Init(IAuthenUserService userService)
        {
            _userService = userService;
        }

        public Route GetRoute(ViewContext context)
        {
            var obj = new Route
            {
                Area = tryeGetRouteInfo("area", context),
                Controller = tryeGetRouteInfo("controller", context),
                Action = tryeGetRouteInfo("action", context),
            };
            return obj;
        }

        private string tryeGetRouteInfo(string key, ViewContext context)
        {
            object value;
            if (context.RouteData.Values.TryGetValue(key, out value))
                return value.ToString().ToLower();
            if (context.RouteData.DataTokens.TryGetValue(key, out value))
                return value.ToString().ToLower();
            return string.Empty;
        }
    }
}